package com.asiainfo.work.service.second.impl;

import com.asiainfo.work.dao.second.ZtBugDao;
import com.asiainfo.work.entity.ZtBug;
import com.asiainfo.work.request.zentao.BugRequest;
import com.asiainfo.work.service.second.ZtBugService;
import com.asiainfo.work.utils.BaseResponse;
import com.asiainfo.work.utils.PageResponse;
import com.asiainfo.work.utils.PageResponseUtil;
import com.asiainfo.work.vo.BugClosedConsumeTimeStatistics;
import com.asiainfo.work.vo.BugClosedStatistics;
import com.asiainfo.work.vo.BugConsumeTimeStatistics;
import com.asiainfo.work.vo.BugStatistics;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ZtBugServiceImpl extends ServiceImpl<ZtBugDao, ZtBug> implements ZtBugService {

    @Autowired
    private ZtBugDao ztBugDao;

    /**
     * 解决禅道bug消耗时间
     * @param bugRequest 查询条件
     * @return分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<BugConsumeTimeStatistics>> queryConsumeBugTime(BugRequest bugRequest) {
        log.info("[queryConsumeBugTime] 参数: taskRequest={}",  new Gson().toJson(bugRequest));
        IPage<BugConsumeTimeStatistics> plusPage = new Page<>(bugRequest.getPage(),
                bugRequest.getSize());
        String endTime = bugRequest.getEndTime();
        String startTime = bugRequest.getStartTime();
        List<String> accountList = bugRequest.getAccount();
        List<BugConsumeTimeStatistics> resultList = ztBugDao.queryConsumeBugTime(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }

    /**
     * 已经关闭的bug消耗时间
     * @param bugRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<BugClosedConsumeTimeStatistics>> queryClosedBugConsumeTime(BugRequest bugRequest) {
        log.info("[queryClosedBugConsumeTime] 参数: taskRequest={}",  new Gson().toJson(bugRequest));
        IPage<BugClosedConsumeTimeStatistics> plusPage = new Page<>(bugRequest.getPage(),
                bugRequest.getSize());
        String endTime = bugRequest.getEndTime();
        String startTime = bugRequest.getStartTime();
        List<String> accountList = bugRequest.getAccount();
        List<BugClosedConsumeTimeStatistics> resultList = ztBugDao.queryClosedBugConsumeTime(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }

    /**
     * 统计总bug数
     * @param bugRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<BugStatistics>> queryBugCount(BugRequest bugRequest) {
        log.info("[queryBugCount] 参数: taskRequest={}",  new Gson().toJson(bugRequest));
        IPage<BugStatistics> plusPage = new Page<>(bugRequest.getPage(),
                bugRequest.getSize());
        String endTime = bugRequest.getEndTime();
        String startTime = bugRequest.getStartTime();
        List<String> accountList = bugRequest.getAccount();
        List<BugStatistics> resultList = ztBugDao.queryBugCount(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }

    /**
     * 统计已经关闭的bug数
     * @param bugRequest 查询条件
     * @return 分页返回任务数
     */
    @Override
    public BaseResponse<PageResponse<BugClosedStatistics>> queryBugClosedCount(BugRequest bugRequest) {
        log.info("[queryBugClosedCount] 参数: taskRequest={}",  new Gson().toJson(bugRequest));
        IPage<BugClosedStatistics> plusPage = new Page<>(bugRequest.getPage(),
                bugRequest.getSize());
        String endTime = bugRequest.getEndTime();
        String startTime = bugRequest.getStartTime();
        List<String> accountList = bugRequest.getAccount();
        List<BugClosedStatistics> resultList = ztBugDao.queryBugClosedCount(startTime, endTime, accountList, plusPage);

        plusPage.setRecords(resultList);
        return BaseResponse.success(PageResponseUtil.getPageResponse(plusPage));
    }
}
